<template>
	<view style="flex: 1;">
		<view style="position: absolute;top: 0;right: 0;bottom: 0;left: 0;background-color: ">
			<image src="@/static/public/background.png" mode="widthFix" style="width: 750rpx;" />
			<view style="flex: 1;background-color: #FBFDFC;"></view>
		</view>
		<scroll-view style="flex: 1;" scroll-y="true" show-scrollbar="false">
			<view class="header">
				<view class="back">
					<image class="image" src="@/static/public/arrow_left_000000.png" mode="widthFix" @click="goBack">
					</image>
				</view>
				<view class="problem">遇到问题</view>
			</view>
			<view class="from">
				<view class="title"><text class="text">欢迎登录！</text></view>
				<view class="phone">
					<view class="tips"><text class="text">+86</text></view>
					<view class="input">
						<up-input placeholder="请输入手机号" border="none" v-model="phone"
							style="padding-left: 20rpx;"></up-input>
					</view>
				</view>
				<view class="captcha" v-if="way">
					<view class="input">
						<up-input placeholder="请输入验证码" border="none" v-model="captcha"
							style="padding-left: 20rpx;"></up-input>
					</view>
					<view class="tips"><text class="text" @click="getCaptcha">{{captchaText}}</text></view>
				</view>
				<view class="password" v-else>
					<view class="input">
						<up-input placeholder="请输入密码" border="none" v-model="password" :password="passwordShow"
							style="padding-left: 20rpx;"></up-input>
					</view>
					<view class="tips" @click="passwordShow = !passwordShow">
						<image class="image" src="@/static/login/eye-close-C5C5C5.png" mode="widthFix"
							v-if="passwordShow"></image>
						<image class="image" src="@/static/login/eye-open-C5C5C5.png" mode="widthFix" v-else></image>
					</view>
				</view>
			</view>
			<view class="feature">
				<view class="agreement">
					<up-checkbox size="16" name="agree" usedAlone v-model:checked="agreement"></up-checkbox>
					<text class="text">我已阅读并同意xxxx协议</text>
				</view>
				<view class="submit" :style="(phone=='')||((way==true&&captcha=='')||(way==false&&password==''))?
				{'backgroundColor': '#ECF8D4','color': '#C5C5C5'}:{'backgroundColor': '#C0F04B','color': '#FFFFFF'}
				" @click="login">验证并登录</view>
				<view class="wayAndRetrieve">
					<view class="way" @click="way = !way">
						<text class="text" v-if="way">密码登录</text>
						<text class="text" v-else>验证码登录</text>
					</view>
					<view class="placeholder"></view>
					<view class="retrieve">找回密码</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>
<script setup>
	import { ref } from 'vue';
	import api from '@/utlis/api.js';
	import { onShow } from '@dcloudio/uni-app';
	import { useStore } from 'vuex';
	import { userInfo } from '@/utlis/config';


	const phone = ref(""); //手机号
	const captcha = ref(""); //验证码
	const password = ref(""); //密码
	const agreement = ref(false); //是否勾选协议
	const way = ref(true); //登录方式
	const passwordShow = ref(true); //是否显示密码
	const captchaText = ref("获取验证码");
	const captchaTime = ref(299); //验证码倒计时
	const captchaCountdown = ref(); //验证码开关
	const loginTheLock = ref(false);
	const goBack = () => {
		uni.navigateBack({
			delta: 1
		});
	}

	const login = () => {
		if (loginTheLock.value) {
			return;
		}
		if ((phone == '') || ((way == true && captcha == '') || (way == false && password == ''))) {
			return;
		}
		if (!agreement.value) {
			uni.showToast({
				title: '请阅读并勾选用户协议',
				icon: 'none',
				duration: 2000
			});
			return;
		}
		loginTheLock.value = true;
		if (way.value) {
			api("app/smsLogin", "POST", { "phone": phone.value, "smsCode": captcha.value }).then(res => {
				loginResult(res)
			});
		} else {
			api("app/pwdLogin", "POST", { "phone": phone.value, "password": password.value }).then(res => {
				loginResult(res)
			});
		}
	}
	const loginResult = (res) => {

		loginTheLock.value = false;
		uni.showToast({
			title: res.msg,
			icon: 'none',
			duration: 2000
		});
		if (res.code == 200) {

			uni.removeStorageSync("captchaTimeDate");
			uni.$store.commit("setToken", res.data.token)
			userInfo().then(data => {
				console.log(data)
				uni.$store.commit("setUser", data)
				uni.$store.commit("initSocket")
				console.log(data)
				uni.switchTab({ url: "/pages/mine/index" });
			});
		}
	}
	//获取验证码
	const getCaptchaTheLock = ref(false)
	const getCaptcha = () => {
		if (getCaptchaTheLock.value) {
			return;
		}else{
			if (phone.value == "") {
				uni.showToast({
					title: '手机号不能为空',
					icon: 'none',
					duration: 2000
				});
			} else {
				getCaptchaTheLock.value = true;
				api("captchaSms", "GET", { "phonenumber": phone.value }).then(res => {
					getCaptchaTheLock.value = false;
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 2000
					});
			
					if (res.code == 200) {
						uni.setStorageSync("captchaTimeDate", new Date().getTime());
						captchaCountdown.value = setInterval(captchaCountdownFunction, 1000);
					}
				})
			
			}
		}
	
	}
	const captchaCountdownFunction = () => {
		captchaText.value = captchaTime.value + "秒后重新获取";
		captchaTime.value--;
		if (captchaTime.value <= 0) {
			clearInterval(captchaCountdown.value);
			captchaTime.value = 299
			captchaText.value = "重新获取";
			uni.removeStorageSync("captchaTimeDate");

		}
	}
	onShow(() => {
		let captchaTimeDate = uni.getStorageSync("captchaTimeDate");
		if (captchaTimeDate != "") {
			getCaptchaTheLock.value = true;
			let remainingTime = (new Date().getTime() / 1000) - (captchaTimeDate / 1000);
			if (remainingTime < 299) {
				captchaTime.value = Math.floor(299 - remainingTime);
				captchaCountdown.value = setInterval(captchaCountdownFunction, 1000);
			} else {
				uni.removeStorageSync("captchaTimeDate");
			}
		}
	})
</script>
<style lang="scss">
	@import "@/uni.scss";

	.header {
		padding: 85rpx 30rpx 0 30rpx;
		display: flex;
		flex-wrap: wrap;

		.problem {
			text-align: right;
		}

		.back {
			text-align: left;
		}

		.back,
		.problem {
			width: 345rpx;

			// text-align: left;
			.image {
				width: 20rpx;
			}

		}
	}

	.from {
		margin-top: 105rpx;
		padding: 0 30rpx 50rpx 30rpx;
		height: 330rpx;

		.phone,
		.password,
		.captcha {

			box-shadow: 0 0 10rpx rgba(0, 0, 0, 0.1);
			background-color: #FFFFFF;
			margin-top: 40rpx;
			border-radius: 15rpx;
			display: flex;
			flex-wrap: wrap;
			box-sizing: border-box;
			padding: 25rpx 0;
			height: 100rpx;
			text-align: center;
		}


		.phone .tips {
			width: 130rpx;
			border-right: #C5C5C5 2rpx solid;
			box-sizing: border-box;
		}

		.captcha .tips {
			width: 300rpx;
			border-left: #C5C5C5 2rpx solid;
			box-sizing: border-box;
		}

		.phone .tips .text,
		.captcha .tips .text {
			text-align: center;
			line-height: 50rpx;
			color: #C5C5C5;
		}

		.captcha .input {
			width: 390rpx;
		}

		.phone .input,
		.password .input {
			width: 560rpx;
		}

		.password .tips {
			width: 130rpx;
			justify-content: center;
			align-items: center;

			.image {
				width: 40rpx;
			}
		}


	}

	.feature {
		padding: 0 30rpx;
		box-sizing: border-box;

		.agreement {
			height: 70rpx;
			display: flex;
			flex-wrap: wrap;


			.text {
				font-size: 30rpx;
				line-height: 70rpx;
				color: #C5C5C5;
			}
		}

		.submit {
			text-align: center;
			line-height: 90rpx;
			border-radius: 15rpx;
			font-size: 30rpx;
		}

		.wayAndRetrieve {
			margin-top: 20rpx;
			display: flex;
			flex-wrap: wrap;
			box-sizing: border-box;

			.text {
				font-size: 30rpx;
			}

			.way,
			.placeholder,
			.retrieve {
				width: 230rpx;
			}

			.retrieve {
				text-align: right;
			}
		}
	}
</style>